package edu.mit.simile.longwell.login;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.vocabulary.RDF;

import edu.mit.simile.longwell.LongwellServer;

public class LoginUtility {
	
	public static void saveLogin(String username){
		String dul = "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#";
		String bcc = "http://www.provincia.tn.it/ont/bcc.owl#";
		String xsd = "http://www.w3.org/2001/XMLSchema#";
		
		DateFormat dateFormatURI = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss");
		DateFormat dateFormatValue = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String day = dateFormatValue.format(date).substring(0, 10)+"T";
		String hour = dateFormatValue.format(date).substring(11,dateFormatValue.format(date).length());
		Model model = TDBFactory.createModel();
		model.setNsPrefix("dul",dul);
		model.setNsPrefix("bcc", bcc);
		model.setNsPrefix("xsd", xsd);
		Resource userResourceURI = model.createResource("http://www.okkam.it/bcc/login/"+username+"/"+dateFormatURI.format(date));
		//System.out.println(userResourceURI.toString());
		Property userLogin = model.createProperty(bcc,"UserLogin");
		Property hasParticipant = model.createProperty(dul, "hasParticipant");
		Property hasEventDate = model.createProperty(dul, "hasEventDate");
		
		userResourceURI.addProperty(RDF.type, userLogin);
		userResourceURI.addProperty(hasParticipant, username);
		userResourceURI.addProperty(hasEventDate, model.createTypedLiteral(day+hour, XSDDatatype.XSDdateTime));
		//Model outputModel = LongwellServer.getBCCDataset().getDefaultModel();
		Model outputModel = LongwellServer.getBCCDataset().getNamedModel("http://www.provincia.tn.it/bcc/resource/bcc-utenti");
		outputModel.add(model);
		outputModel.commit();
	}
	
	public static void saveLogout(String username){
		if(username!=null){
			String dul = "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#";
			String bcc = "http://www.provincia.tn.it/ont/bcc.owl#";
			String xsd = "http://www.w3.org/2001/XMLSchema#";
			
			DateFormat dateFormatURI = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss");
			DateFormat dateFormatValue = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			String day = dateFormatValue.format(date).substring(0, 10)+"T";
			String hour = dateFormatValue.format(date).substring(11,dateFormatValue.format(date).length());
			Model model = TDBFactory.createModel();
			model.setNsPrefix("dul",dul);
			model.setNsPrefix("bcc", bcc);
			model.setNsPrefix("xsd", xsd);
			Resource userResourceURI = model.createResource("http://www.okkam.it/bcc/login/"+username+"/"+dateFormatURI.format(date));
			//System.out.println(userResourceURI.toString());
			Property userLogin = model.createProperty(bcc,"UserLogout");
			Property hasParticipant = model.createProperty(dul, "hasParticipant");
			Property hasEventDate = model.createProperty(dul, "hasEventDate");
			
			userResourceURI.addProperty(RDF.type, userLogin);
			userResourceURI.addProperty(hasParticipant, username);
			userResourceURI.addProperty(hasEventDate, model.createTypedLiteral(day+hour, XSDDatatype.XSDdateTime));
			//Model outputModel = LongwellServer.getBCCDataset().getDefaultModel();
			Model outputModel = LongwellServer.getBCCDataset().getNamedModel("http://www.provincia.tn.it/bcc/resource/bcc-utenti");
			outputModel.add(model);
			outputModel.commit();			
		}
	}	
}
